import util from 'commons/util';
import config from 'commons/config';
import { share } from "api_xukaiyu/app/wechat";

// 微信分享sdk配置
const wxConfigData = {
    debug: false,
    appId: config.wechat.appId,
    timestamp: "",
    nonceStr: "",
    signature: "",
    jsApiList: [
        "onMenuShareTimeline",
        "onMenuShareAppMessage",
        "onMenuShareQQ",
        "onMenuShareWeibo",
        "onMenuShareQZone",
        'startRecord',
        'stopRecord',
        'onVoiceRecordEnd',
        'playVoice',
        'pauseVoice',
        'stopVoice',
        'onVoicePlayEnd',
        'uploadVoice',
        'translateVoice'
    ]
}

// 环境
let env = "";
// 是否初始化过分享
let hasInitShare = false;

// 设置微信分享
const wxShare = (shareData = {}) => {
    const creatShare = function (data) {
      // 微信内非小程序环境
        if (env == "wechat") {
            // 设置
            const set = function (data) {
                wx.ready(() => {
                    wx.onMenuShareAppMessage(data); //分享给朋友
                    wx.onMenuShareTimeline(data); //分享到朋友圈
                    wx.onMenuShareQQ(data); //分享到QQ
                    wx.onMenuShareWeibo(data); //分享到微博
                    wx.onMenuShareQZone(data); //分享到QZone 
                });
            }

            if (!hasInitShare) {
                // 初始化微信分享
                share({
                    appId: config.wechat.appId,
                    shareUrl: location.href
                }).then(res => {
                    // 设置初始化信息
                    wxConfigData.timestamp = res.data.timestamp;
                    wxConfigData.nonceStr = res.data.nonce;
                    wxConfigData.signature = res.data.signature;
                    // wxConfigData.debug= true;
                    if (typeof wx !== "undefined") {
                        hasInitShare = true;// 修改初始化状态
                        wx.config(wxConfigData);// 初始化配置
                        set(data);// 设置分享内容
                    }
                })
            } else {
                if (typeof wx !== "undefined") {
                    set(data);// 设置分享内容
                }
            }
        }
        if (env == "miniprogram") {
            if((location.href.includes('fightRedEnvelope.html')||location.href.includes('invite.html')||location.href.includes('inviteNew/index.html'))&&!data.link.includes('goods.html#/Detail')){
                data.link=`/pages/webpage/webpage?url=${encodeURIComponent(data.link)}`;
            }
            if(data.link.includes('goods.html#/Detail')){
                data.link=`/pages/goodsDetail/goodsDetail?id=${data.link.split('/').pop()}`
            }
            // 小程序内
            wx.miniProgram.postMessage({ data: { foo: 'bar', shareData: data } })
        }
        
    }
    
    // 已知环境，设置分享，未知环境，先检查环境再设置分享
    if (!!env) {
        let sData = Object.assign(config.wechat.shareData, shareData);
        creatShare(sData);
    } else {
        util.checkWechatEnv((res) => {
            env = res;
            let sData = Object.assign(config.wechat.shareData,env=='miniprogram'?{imgUrl: "https://oss.weidai.com.cn/6aDFDyreAW.png"}:{}, shareData);
            creatShare(sData);
        })
    }
}

export default wxShare;